package com.robot.base.utils;


import com.robot.base.util.StringUtil;

public class DesensitizedUtils {

    /**
     * 姓名脱敏
     *
     * @param fullName
     * @return
     */
    public static String desensitizedName(String fullName) {
        if (StringUtil.isValid(fullName)) {
            char[] r = fullName.toCharArray();
            if (r.length == 1) {
                return fullName;
            }
            if (r.length == 2) {
                String name = StringUtil.left(fullName, 1);
                return StringUtil.rightPad(name, StringUtil.length(fullName), "*");
            }
            if (r.length > 2) {
                return fullName.replaceFirst(fullName.substring(1, r.length - 1), "*");
            }
        }
        return fullName;
    }

    /**
     * 手机号脱敏
     *
     * @param phoneNumber
     * @return
     */
    public static String desensitizedPhoneNumber(String phoneNumber) {
        if (StringUtil.isValid(phoneNumber)) {
            phoneNumber = phoneNumber.replaceAll("(\\w{3})\\w*(\\w{4})", "$1****$2");
        }
        return phoneNumber;
    }
}
